package com.lanou.bookstore.order.web.servlet;

import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.cart.domain.CartItem;
import com.lanou.bookstore.order.domain.Order;
import com.lanou.bookstore.order.domain.OrderItem;
import com.lanou.bookstore.order.service.OrderSercice;
import com.lanou.bookstore.order.service.OrderSerciceImpl;
import com.lanou.bookstore.user.dao.UserDao;
import com.lanou.bookstore.user.dao.UserDaoImpl;
import com.lanou.bookstore.user.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@WebServlet("/os")
public class OrderServlet extends HttpServlet {

    OrderSercice os = new OrderSerciceImpl();
    UserDao ud = new UserDaoImpl();
    User user = new User();
    Random ra = new Random();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uname = (String)req.getSession().getAttribute("uname");
//        String cartName = uname.concat("cartList");
        List<CartItem> cartList = (List<CartItem>) req.getSession().getAttribute("cartList");

        double num = 0;
        Book book = null;

        double total = 0;
        for (CartItem cart1 : cartList) {
            book = cart1.getBook();
            num = cart1.getNum();

            total = book.getPrice() * num + total;

        }

        // 数据准备

        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd a hh:mm");
//        String uname = (String) req.getSession().getAttribute("uname");

        Order order = new Order();
        // 订单ID
        order.setOid(Integer.toString(ra.nextInt(100) + 1000));
        // 订单生产时间
        order.setOrdertime(ft.format(date));
        // 订单合计
        order.setTotal(total);
        // 订单状态
        order.setState(0);

        user.setUsername(uname);
        List<User> userList = ud.findByUsername(user);
        for (User user1 : userList) {
            // 谁买的    user的id
            order.setUid(user1.getUid());
        }
        os.addOrder(order);



        // 购物车cartList (Cart对象  >   (Book , 数量 )  >   书的信息            )


        for (CartItem cartItem : cartList) {
            OrderItem orderItem = new OrderItem();
            orderItem.setIid(Integer.toString(ra.nextInt(100) + 1000));
            orderItem.setCount(cartItem.getNum());
            orderItem.setSubtotal( cartItem.getNum() * cartItem.getBook().getPrice());
            orderItem.setOid(order.getOid());
            orderItem.setBid(cartItem.getBook().getBid());

            os.addOrderItem(orderItem);
        }


        req.setAttribute("order", order);
        req.getSession().setAttribute("orderMessage", order);

        req.getSession().setAttribute("order", order);
        req.getRequestDispatcher("/jsps/order/desc02.jsp").forward(req, resp);

        List<CartItem> arrayList = new ArrayList<CartItem>();
        //todo 这儿改过了
        req.getSession().setAttribute("cartList", arrayList);

    }
}
